﻿using System;
using System.Collections.Generic;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using CRMBiz;
using Newtonsoft.Json;
using CRMWeb.Utilities.Context;
using System.Diagnostics;



namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for ApplicationSACC
    /// </summary>
    public class ApplicationSACC : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        public void GetVerificationStatusData(int app_id)
        {
            DataSet ds;
            try
            {
                ds = ApplicationSACCHandler.SelectVerificationStatusData(app_id);
                if (ds.Tables[0].Rows.Count == 1)
                    context.Response.Write(JsonConvert.SerializeObject(ds.Tables[0]));
                else
                    context.Response.Write("0");
            }
            catch (Exception ex)
            {
                context.Response.Write("1");
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void saveVerificationStatusData(int VerifyStatusID, int app_id, string idcheck_status, int bankretrieve_attempts, string bankretrieve_status, string payslip_status, string creditehistory, string incomeexpense, string phoneverification, string contractprepared, string contractsigned, string fundtransferred, string historyOperator)
        {

            try
            {
                ApplicationSACCHandler.saveVerificationStatus(app_id, idcheck_status, bankretrieve_attempts, bankretrieve_status, payslip_status, creditehistory, incomeexpense, phoneverification, contractprepared, contractsigned, fundtransferred, historyOperator);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void updateDownloadCount(string fieldName, int newCount, int app_id)
        {
            bool response = false;
            try
            {
                response = ApplicationSACCHandler.UpdateDownloadCount(fieldName, newCount, app_id);
                context.Response.Write(response);
            }
            catch (Exception ex)
            {
                context.Response.Write("1");
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void getAppFormIDcomesFrom(int app_id)
        {
            DataTable dt;
            try
            {
                dt = ApplicationSACCHandler.getFormIdwithComesFrom(app_id);
                context.Response.Write(JsonConvert.SerializeObject(dt));
            }
            catch (Exception ex)
            {
                context.Response.Write("1");
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
    }
}